package lerry.utils;
import flash.geom.Point;
import flash.utils.Object;

/**
 * ...
 * @author lerry
 */
class MathUtils{
	/**
		 * 两点之间的距离
		 * @param x1
		 * @param y1
		 * @param x2
		 * @param y2
		 * @return 
		 * 
		 */		
		public static function getDistance(x1:Float,y1:Float,x2:Float,y2:Float):Float{
			var dx:Float=x1-x2;
			var dy:Float=y1-y2;
			var dis:Float=Math.sqrt(dx*dx+dy*dy);
			return dis;
		}
		/**
		 * x1,y1相对于x2,y2的角度
		 * @param x1
		 * @param y1
		 * @param x2
		 * @param y2
		 * @param isDegree
		 * @return 
		 * 
		 */		
		public static function getAngle(x1:Float,y1:Float,x2:Float,y2:Float,isDegree:Bool):Float{
			var dx:Float=x1-x2;
			var dy:Float=y1-y2;
			var rad:Float=Math.atan2(dy,dx);
			if(isDegree){
				return degreeRadian(rad,false);
			}
			return rad;
		}
		/**
		 * 角度,弧度互转
		 * @param angle
		 * @param inputDegree
		 * @return 
		 * 
		 */		
		public static function degreeRadian(angle:Float,inputDegree:Bool):Float{
			if(inputDegree)return angle*Math.PI/180;
			return angle*180/Math.PI;
		}
		public static function TO_DEGREE(radian:Float):Float {
			return radian * 180 / Math.PI;
		}
		public static function TO_RADIAN(degree:Float):Float {
			return degree * Math.PI / 180;
		}
		/**
		 * 物体绕目标点作弹性运动
		 * @param target目标距离
		 * @param center
		 * @param fric
		 * @param elasticStrength
		 * @param speed
		 * @param gravity
		 * @return 
		 * 
		 */		
		public static function getElastic(target:Float,center:Float,fric:Float,elasticStrength:Float,speed:Float=0,gravity:Float=0):Float{
			var dis:Float=center-target;
			var a:Float=dis*elasticStrength;
			speed+=gravity;
			speed+=a;
			speed*=1-fric;
			if(Math.abs(speed)<.05)
				speed=0;
			return speed;
		}
		/**
		 * 取得两个值之间的随机数
		 * @param min
		 * @param max
		 * @return 
		 * 
		 */		
		public static function getRandom(min:Float,max:Float):Float{
			return Math.random()*(max-min)+min;
		}
		/**
		 * 直线与圆相交函数
		 * x1,y1直线1上的一点
		 * x2,y2直线2上的一点
		 * @param x1
		 * @param y1
		 * @param x2
		 * @param y2
		 * @param x3
		 * @param y3
		 * @param r
		 * @return 
		 * 
		 */		
		public static function getCutline(x1:Float, y1:Float, x2:Float, y2:Float, x3:Float, y3:Float, r:Float):Object
		{
			var A:Float=(x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1);
			var B:Float=2 * ((x2 - x1) * (x1 - x3) + (y2 - y1) * (y1 - y3));
			var C:Float=x3 * x3 + y3 * y3 + x1 * x1 + y1 * y1 - 2 * (x3 * x1 + y3 * y1) - r * r;
			var bb_plus_4ac:Float=B * B - 4 * A * C;
			if (bb_plus_4ac > 0)
			{
				var sqrt:Float=Math.sqrt(bb_plus_4ac);
				var u1:Float=(-B + sqrt) / 2 / A;
				var u2:Float=(-B - sqrt) / 2 / A;
				var cutx1:Float=x1 + (x2 - x1) * u1;
				var cuty1:Float=y1 + (y2 - y1) * u1;
				var cutx2:Float=x1 + (x2 - x1) * u2;
				var cuty2:Float=y1 + (y2 - y1) * u2;
				var obj:Object={x1:cutx1, y1:cuty1, x2:cutx2, y2:cuty2};
				if (u1 > 0 && u1 < 1)
				{
					return obj;
				}
				if (u2 > 0 && u2 < 1)
				{
					return obj;
				}
			}
			return null;
		}
		/**
		 * 根据两点求直线方程,
		 * 返回的object有k,b两个属性
		 *x1,y1点1
		 * x2,y2点2 
		 * @param x1
		 * @param y1
		 * @param x2
		 * @param y2
		 * @return 
		 * 
		 */		
		public static  function getLine(x1:Float,y1:Float,x2:Float,y2:Float):Object{
			var kk:Float=0;
			var bb:Float=0;
			if(x1!=x2){
				kk=(y2-y1)/(x2-x1);
				bb=y2-kk*x2;
			}
			var object:Object={};
			object.k=kk;
			object.b=bb;
			return object;
		}
		/**
		 * 检查点是否在某条直线上
		 * @param line拥有k,b两个属性的object
		 * @param x
		 * @param y
		 * @return 
		 * 
		 */		
		public static  function checkOnLine(line:Object,x:Float,y:Float):Bool{
			if(y==line.k*x+line.b){
				return true;
			}
			return false;
		}
		/**
		 * 若两条直线相交，返回交点
		 * @param line1拥有k,b两个属性的object
		 * @param line2拥有k,b两个属性的object
		 * @return 
		 * 
		 */		
		public static function getPointOnLines(line1:Object,line2:Object):Point{
			var p:Point=new Point();
			if(line1.k==line2.k){
				return null;
			}
			p.x=(line2.b-line1.b)/(line1.k-line2.k);
			p.y=line1.k*p.x+line1.b;
			return p;
		}
		/**
		 *直线上的两点确定的线段的垂直平分线
		 * x1,y1,x2,y2直线上的两点
		 * @param x1
		 * @param y1
		 * @param x2
		 * @param y2
		 * @return 
		 * 
		 */		
		public static function getPerpen(x1:Float,y1:Float,x2:Float,y2:Float):Dynamic{
			var line:Dynamic=getLine(x1,y1,x2,y2);
			var kk:Float=-1/line.k;
			var bb:Float=(y1+y2)*.5-kk*(x1+x2)*.5;
			var obj:Dynamic={};
			obj.k=kk;
			obj.b=bb;
			return obj;
		}
}